'use strict';
/**
 * Created by FreezeSoul on 2015/10/15.
 */
define([
        'angular',
        'adConfig',
        'app/modules/page/define',
        'app/modules/page/services/page',
        'app/modules/page/controllers/page-delete',
    ],
    function (angular, adConfig) {
        angular.module('ad.page')
            .controller('adPageSearchController', ['$rootScope', '$scope', '$state', '$uibModal', '_', 'Notification', 'adPageEditService'
                , function ($rootScope, $scope, $state, $uibModal, _, Notification, service) {
                    $scope.schemas = [];
                    $scope.isLoading = false;
                    $scope.searchWord = "";

                    $scope.maxSize = 5;
                    $scope.itemsPerPage = adConfig.perPageRowSize;
                    $scope.currentPage = 1;
                    $scope.totalItems = 0;

                    $scope.searchData = function () {
                        $scope.isLoading = true;
                        service.getPage($scope.currentPage, $scope.itemsPerPage, $scope.searchWord).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $scope.pages = obj.data.items;
                                $scope.totalItems = obj.data.count;
                            }
                            $scope.isLoading = false;
                        });
                    };

                    $scope.pageChanged = function () {
                        $scope.searchData();
                    };

                    var deleteItems = function (items) {
                        var modalInstance = $uibModal.open({
                            animation: true,
                            templateUrl: 'app/modules/page/views/page-delete.tpl.html',
                            controller: 'adPageDeleteController',
                            size: 'sm',
                            resolve: {
                                items: function () {
                                    return items;
                                }
                            }
                        });

                        modalInstance.result.then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                Notification.success('删除成功！');
                            }
                            $state.reload();
                        }, function () {
                        });
                    };

                    $scope.deleteSelected = function () {
                        var items = _.filter($scope.pages, function (item) {
                            return item.isSelected;
                        });
                        deleteItems(items);
                    };

                    $scope.deleteRow = function (row) {
                        deleteItems([row]);
                    };

                    $scope.editRow = function (row) {
                        $state.go('editor.page-edit', {pageid: row.page_id});
                    };

                    $scope.searchData();
                }]);
    });